<template>
<div class="search-result">
  <head>
    <meta name="referrer" content="no-referrer"/>
  </head>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    :error.sync="error"
    error-text="加载失败，请点击重试"
    @load="onLoad"
  >
    <van-cell v-for="(article, index) in list" :key="index.art_id">
      <div class="title van-multi-ellipsis--l2">{{ article.title }}</div>
      <div class="img-list">
        <div class="img-wrapper" v-for="(imgSrc, i) in article.cover.images" :key="i">
          <img referrer="no-referrer|origin|unsafe-url" :src="imgSrc">
        </div>
      </div>
      <div class="info">
        <div class="top is-top" v-if="true">置顶</div>
        <div class="author">{{article.aut_name}}</div>
        <div class="comment-count">评论数: {{article.comm_count}}</div>
        <div class="time">{{timestamp2fromNow(article.pubdate)}}</div>
      </div>
    </van-cell>
  </van-list>
</div>
</template>

<script>
import { getSearch } from "@/api/article.js";
import moment from "moment";

export default {
  name: "SearchResult",
  components: {},
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      perPage: 20,
      error: false
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onLoad() {
      try {
        // 1. 请求获取数据
        const { data } = await getSearch({
          // page: this.page, // 页码
          // per_page: this.perPage, // 每页大小
          q: this.searchText // 查询关键词
        });

        // 2. 将数据添加到数组列表中
        const { results } = data.data;
        this.list.push(...results);

        // 3. 将本次加载中的 loading 关闭
        this.loading = false;

        // 4. 判断是否还有数据
        if (results.length) {
          // 如果有，则更新获取下一个数据的页码
          this.page++;
        } else {
          // 如果没有，则将加载状态 finished 设置为结束
          this.finished = true;
        }
      } catch (err) {
        // 展示加载失败的提示状态
        this.error = true;

        // 加载失败了 loading 也要关闭
        this.loading = false;
      }
    },
    timestamp2fromNow(timestamp) {
      return moment(timestamp).fromNow();
    },
    attachImageUrl(srcUrl) {
      if (srcUrl !== undefined) {
        return srcUrl.replace(
          /^(http)[s]*(\:\/\/)/,
          "https://images.weserv.nl/?url="
        );
      }
    }
  }
};
</script>

<style scoped lang="less">
.van-list {
  margin-top: 20px;
  height: 79vh;
  overflow: auto;
}

.title {
  word-break: break-all;
  font-size: 30px;
}

.img-list {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;

  .img-wrapper {
    height: 150px;
    overflow: hidden;
    position: relative;

    img {
      display: block;
      width: 100%;
      height: 100%;
      // center;
    }
  }
}
.info {
  width: 100%;
  overflow: hidden;

  .is-top {
    color: red;
  }

  & > div {
    float: left;
    font-size: 10px;
    margin-right: 12px;
    color: #d6d6d6;
  }
}
.van-popup {
  margin-top: 100px;
}
</style>
